package com.example.graphmovieapp.config;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

/**
 * Neo4j Testcontainers配置类
 * 用于在测试中自动启动Neo4j容器
 */
@TestConfiguration
@Testcontainers
public class Neo4jTestContainerConfig {

    @Container
    static Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>(
            DockerImageName.parse("neo4j:5.15"))
            .withAdminPassword("password")
            .withReuse(true);

    @DynamicPropertySource
    static void neo4jProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.neo4j.uri", neo4jContainer::getBoltUrl);
        registry.add("spring.neo4j.authentication.username", () -> "neo4j");
        registry.add("spring.neo4j.authentication.password", () -> "password");
    }

    @BeforeAll
    static void setUp() {
        neo4jContainer.start();
    }

    @AfterAll
    static void tearDown() {
        if (neo4jContainer.isRunning()) {
            neo4jContainer.stop();
        }
    }
}